//
//  ListViewContentOnline.m
//  CMS
//
//  Created by Vuong Van Khanh on 10/29/12.
//  Copyright (c) 2012 Rikkei. All rights reserved.
//

#import "ListViewContentOnline.h"
#import "SBJson.h"
@implementation ListViewContentOnline
@synthesize tableData;

- (void) alertStatus:(NSString *)msg :(NSString *)title
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil, nil];
    
    [alertView show];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}


- (void) viewDidLoad {
    [super viewDidLoad];
    @try{
        NSURL *url=[NSURL URLWithString:@"http://192.168.1.24:8080/login/listcontentview.php"];
        
        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        
        NSURLRequest *request=[NSURLRequest requestWithURL:url
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:30.0];
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
        
        NSLog(@"Response code: %d", [response statusCode]);
        if ([response statusCode] >=200 && [response statusCode] <300)
        {
            NSString *responseData = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];                   
             NSLog(@"%@", responseData);
            SBJsonParser *jsonParser = [SBJsonParser new];
            
            NSArray *jsonData = (NSArray *) [[jsonParser objectWithString:responseData error:nil] objectForKey:@"contents"];
            tableData = [[NSMutableArray alloc] init];
            for(NSDictionary *object in jsonData)
            {
//                NSLog(@"id: %@ name: %@",[object objectForKey:@"id"],[object objectForKey:@"name"]);
                [tableData addObject:[object objectForKey:@"name"]]; 
                
            }
             
//            NSLog(@"%d",id);
        }
        else
        {
            NSLog(@"Connect tion error!");
            [self alertStatus:@"Connection Failed" :@"Connection error!"];
        }

    }@catch(NSException *ex){
        NSLog(@"Error: %@", ex.description);
        [self alertStatus:@"Error when access to server. Please contact to administrator" :@"Error"];
    }
   
}

#pragma mark - View lifecycle
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [tableView dequeueReusableCellWithIdentifier:@"cellInListContentOnline"];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellInListContentOnline"];
    }
    NSLog(@"%@",tableData);
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

- (IBAction)btnLogoutClicked:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)backbtnClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
